﻿using System.Windows;

namespace SHomeWorkshop.LunarPresentation.Commands
{
    public partial class Command
    {
        public static void DeleteLine()
        {
            if (Globals.selectedEditor == null) return;
            if (Globals.selectedEditor.SelectedExpression == null) return;

            LunarPresentation.Expressions.Expression curEx
                = Globals.selectedEditor.SelectedExpression;

            if ((curEx is Expressions.GridPanel) == false)
            {
                if (curEx.ParentPanel != null && curEx.ParentPanel is Expressions.GridPanel)
                {
                    curEx = curEx.ParentPanel;
                }
                else
                {
                    MessageBox.Show("请先选中一个网格子面板，然后再执行此一操作。",
                        Globals.appName,
                        MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;//只有网格式子面板或其子级公式才能进行操作。
                }
            }

            ModifingItem mi = new ModifingItem() { OldSelectedExID = curEx.ExID };

            Expressions.GridPanel gp = curEx as Expressions.GridPanel;
            //Expressions.SubPanel topPanel = gp.TopSubPanel;
            Expressions.Grid grid = gp.ParentExpression as Expressions.Grid;
            string oldXmlText = grid.XmlData.OuterXml;

            if (grid != null)
            {
                if (grid.LinesCount < 2)
                {
                    MessageBox.Show("网格式中至少要保留[1行×1列]。",
                        Globals.appName,
                        MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }

                if (grid.DeleteLine(gp, mi))
                {
                    //if (topPanel != null)
                    //{
                    //    Globals.selectedEditor.SelectedExpression = topPanel;
                    //}
                    //else
                    //{
                    //    Globals.selectedEditor.SelectedExpression = grid;
                    //}

                    grid.Editor.Do(mi);
                }
            }
        }
    }
}
